package cn.blankchn.www.util;

import cn.blankchn.www.entity.HandleResult;
import cn.blankchn.www.entity.Part;
import cn.blankchn.www.entity.Time;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 2018/4/2.
 */
public class HandleFactory {

    public static ArrayList<HandleResult> handle(List<Part> list_part, int startNum){

        ArrayList<HandleResult> list_results = new ArrayList<HandleResult>();

        for(Part part : list_part){

            HandleResult result = new HandleResult();
            result.setNum(startNum++);

            //平均速度
            result.setAvgSpeed(handleAvgSpeed(part));
            //平均加速度
            result.setAvgAcce(handleAvgAcce(part));
            //平均减速度
            result.setAvgDown(handleAvgDown(part));
            //持续时间
            result.setPersistentTime(part.getList().size());
            //最大加速度
            result.setMaxAcce(handleMaxAcce(part));
            //最大减速度
            result.setMaxDown(handleMaxDown(part));
            //加速时间
            result.setAcceTime(part.getList_up().size());
            //怠速时间
            result.setZeroTime(part.getList_zero().size());
            //减速时间
            result.setDownTime(part.getList_down().size());
            //最大速度
            result.setMaxSpeed(handleMaxSpeed(part));
            //vs
            result.setVs(handleVs(part,result.getAvgSpeed()));
            //va
            result.setVa(handleVa(part));
            //匀速行驶时间
            result.setUniformTime(part.getList_avg().size());
            list_results.add(result);

        }

        return list_results;
    }

    private static double handleVa(Part part) {

        double avgAc = 0;
        for(Time time : part.getList()){
            avgAc += time.getAcceleration();
        }

        avgAc /= part.getList().size();

        double count = 0;
        for(Time time : part.getList()){
            count += (time.getAcceleration()-avgAc)*(time.getAcceleration()-avgAc);
        }

        return Math.sqrt(count/part.getList().size());

    }

    private static double handleVs(Part part, double avgSpeed) {

        double count = 0;
        for(Time time : part.getList()){
            count += (time.getSpeed()-avgSpeed)*(time.getSpeed()-avgSpeed);
        }

        return Math.sqrt(count/part.getList().size());

    }

    private static double handleMaxSpeed(Part part) {
        double maxSpeed = 0;
        for(Time time : part.getList()){
            if(time.getSpeed()>maxSpeed){
                maxSpeed = time.getSpeed();
            }
        }

        return maxSpeed;
    }


    private static double handleMaxDown(Part part) {

        double maxDown = 0;
        for(Time time : part.getList_down()){
            if(time.getAcceleration() < maxDown){
                maxDown = time.getAcceleration();
            }
        }

        return maxDown;
    }

    private static double handleMaxAcce(Part part) {

        double maxAcce = 0;
        for(Time time : part.getList_up()){
            if(time.getAcceleration()>maxAcce){
                maxAcce = time.getAcceleration();
            }
        }

        return maxAcce;

    }

    private static double handleAvgDown(Part part) {

        double downAll = 0;
        for(Time time : part.getList_down()){
            downAll += time.getAcceleration();
        }

        return downAll/part.getList_down().size();

    }

    private static double handleAvgAcce(Part part) {
        double acceAll = 0;
        for(Time time : part.getList_up()){
            acceAll += time.getAcceleration();
        }

        return acceAll/part.getList_up().size();
    }

    public static double handleAvgSpeed(Part part){
        double speedAll = 0;
        for(Time time : part.getList()){
            speedAll+= time.getSpeed();
        }

        return speedAll/part.getList().size();
    }


}
